Flash Article

Building a Tip of the Day Application (Part 3): Submitting Tips to the Web Service


Table of Contents

  1. Introduction
  2. Binding Data to the Submit a Tip Interface
  3. Formatting Dates
  4. Building the Calendar Browse Interface
  5. Searching for Tips
  6. Displaying a Tip from the Search Results
  7. Applying a Style Sheet

Searching for Tips

Building the searchform and searchresult systems are perhaps the most complex portions of the application. Sending the search to the server is a simple process: You'll simply bind the keyword_txt TextInput field to a WebServiceConnector component and use a behavior to trigger the web service when a user clicks the Search button. Then you'll process the returned XML results.

Calling the Search

Now you will add the code to call the search from the SWF file. Bind the search keyword text entry field to its corresponding parameter in the web service as follows:

  1. Select the presentation screen and open the Web Services panel (Control+Shift+F10). Expand the tipService, right-click getTipsByKeyword, add a method call, and close the Web Services panel.
  2. Move the new WebServiceConnector component off the Stage with the other data components and assign it an instance name of getTipsByKeyword_wsc.
  3. Open the Bindings tab in the Component Inspector panel and click the Add Binding button to open the Add Binding dialog box.
  4. Select the Keyword : string from the params object and click OK.
  5. Bind params.Keyword to the keyword_txt TextInput instance on the searchform screen. In the Bindings tab, select params.Keyword, and then double-click the field to the right of bound to. The Bound To dialog box opens. Find the keyword_txt instance in the tree and select it. (See Figure 3.) Click OK to close the Bound To dialog box.

    Binding params.keyword to keyword_txt.

    Figure 3. Binding params.keyword to keyword_txt.

Add the Trigger Data Source behavior to the searchform screen as follows:

  1. On the searchform screen, select the Search button. Open the Behaviors panel, click the Add Behavior button and select Data > Trigger Data Source from the contextual menu. In the Select Data Source component pane of the Trigger Data Source dialog box, select the getTipsByKeyword_wsc instance and click OK.
  2. Make sure the Trigger Data Source behavior is above the Go to Slide behavior for the Search button on the searchform screen.

    Because this Search button already has a behavior placed on it, confirm that the Trigger Data Source behavior appears before the Go to Slide behavior in the list in the Behaviors panel. To reorder behaviors, you can select the behavior you want in the Behaviors panel and click the Move Up or Move Down button in the upper right corner of the panel.

    Behaviors for the Search button on the searchform screen

    Figure 4. Behaviors for the Search button on the searchform screen

Receiving XML Results from a Web Service

If you are receiving XML from a web service, it is easy to consume the data by converting the packet into a Flash XML object. Just save the results as an XML packet in the DataHolder component, and then bind data to the XML packet.

Also, before you can bind the data from the XML packet, you have to import an XML schema so Flash can build the structure of the XML document. In this section, you will save and import a sample schema from XML generated by a trace statement that you pull from the Output panel.

  1. Select the presentation screen and click the DataHolder instance. Open the Component Inspector panel and click the Schema tab.
  2. Click the Add a Component Property (+) button in the upper left corner and, in the lower pane, set the field name for the new field to keywordresults. Set the field’s data type to XML.
  3. Select the getTipsByKeyword_wsc instance, and open the Actions panel. Enter the following ActionScript to trace results.

    on (result) {
        trace(this.results);
    }
  4. Test your movie. When you do a keyword search, Macromedia Flash displays the XML packet sent back from the server to the Output window. Make sure you have multiple results and not a single result. (You can try searching for a blank string to return the maximum number of records.)
  5. Copy the XML packet from the Output window and save it to an XML file on your hard drive.
  6. Import the schema from the XML file you just saved as follows:
    1. Select the DataHolder instance on the presentation screen.
    2. With the keywordresults property selected in the upper Schema pane of the Component Inspector panel, click the Import a Schema from a Sample XML File button (located near the upper right corner of the Component Inspector panel, just below the Schema tab). This launches a file browser where you can locate the XML file you just saved on your hard drive.
    3. Choose the XML file you saved and click Open. Now you can expand the schema for the keywordresults property and see its structure.
  7. Remove the white space from the XML and set the value of keywordresults to the XML packet using ActionScript as follows:
    1. Click the getTipsByKeyword_wsc instance on the Stage.
    2. Open the Actions panel and replace the existing code with the code below:

      on (result) {
          XML.prototype.ignoreWhite = true;
          this._parent.dataHolder_dh.keywordresults = new XML(this.results);
      }
      
  8. The first line of code tells Macromedia Flash to ignore white space in XML documents. (When working with XML, Flash can be sensitive to excessive white space; this command tells Flash to ignore all nodes that contain only white space and no data.) The second line of code sets the value of the keywordresults property in the DataHolder instance to the results of the XML packet. Because you’re using the XML() constructor, Flash converts the packet into a Flash XML object, which you can bind and manipulate in Flash.

  9. New dataHolder_dh binding to keywordresults.results.tip

    Figure 5. New dataHolder_dh binding to keywordresults.results.tip

  10. Select the dataHolder_dh instance on the presentation screen and open the Bindings tab of the Component Inspector panel. Add a new binding for keywordresults > results > tip. Set the direction of the binding to out.

  11. Double-click the bound to field in the lower pane of the Bindings tab to open the Bound To dialog box. In the Component Path pane, select the results_ls List component and then select dataProvider as the Schema location.

    By setting a List component’s dataProvider property, you are able to set all of the list’s items at once. This means that once you've bound the List component to the tip array, the List contains each tip passed back from the web service.

    If you test the SWF file now, search, and receive multiple results, you will see that all of the fields from the XML document are merged into the label property for each record. To rearrange the fields before they are bound to the List component then, use a formatter. You can find a formatter in the Bindings tab of the Component Inspector panel.

    Formatter for the keywordresults.results.tip data object.

    Figure 6. Formatter for the keywordresults.results.tip data object.

  12. Set the formatter to Rearrange Fields as shown in Figure 6. This rearranges the fields from the XML object before it is bound to the List component. For this example, set the Fields definition value to the following:

    data=TipDateTime;label=TipTitle

    You are binding the data property of the List component to the TipDateTime field from the web service’s returned XML. Likewise, you are setting the label for the List component to the TipTitle from the DataHolder component.